package com.matrix.hellokafka.hellowold;

import com.matrix.hellokafka.util.ConfigUtil;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class HelloKafkaProducer {

    public static void main(String[] args) {
        Properties properties = ConfigUtil.getProducerProperties();

        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);

        try {
            ProducerRecord<String, String> record;
            for (int i = 0; i < 400; i++) {
                record = new ProducerRecord<>("my-topict", String.valueOf(i), "jyy==>"+i);
                producer.send(record); // 发后即忘记（fire-and-forget）会有重试
                System.out.println(i + "，message is sent");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.close();
        }
    }

}
